package cn.net.wanji.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 文件处理工具类
 */
@Slf4j
public class FileUtils {
/*
    *//**
     * 根据指定文件路径
     * 删除指定文件夹
     *//*
    public boolean deleteFolder(String folderPath) {
        File folder = new File(folderPath);
        if (folder.exists()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        // 递归调用删除文件夹
                        deleteFolder(file.getAbsolutePath());
                    } else {
                        // 删除文件
                        file.delete();
                    }
                }
            }
        }
        // 删除空文件夹或已清空的文件夹
        return folder.delete();
    }*/

    public static File findEarliestFolder(String rootFolder) {
        File root = new File(rootFolder);
        File earliestFolder = null;
        Date earliestDate = new Date(Long.MAX_VALUE);
        if (root.isDirectory()) {
            File[] yearMonthFolders = root.listFiles();
            if (yearMonthFolders != null) {
                for (File yearMonthFolder : yearMonthFolders) {
                    if (yearMonthFolder.isDirectory()) {
                        File[] dayFolders = yearMonthFolder.listFiles();
                        if (dayFolders != null && dayFolders.length > 0) {
                            String folderName = dayFolders[0].getParentFile().getName();
                            Date currentDate = parseDate(folderName);
                            if (currentDate != null && currentDate.before(earliestDate)) {
                                earliestDate = currentDate;
                                earliestFolder = dayFolders[0];
                            }
                        }
                    }
                }
            }
        }
        return earliestFolder;
    }

    private static Date parseDate(String folderName) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        try {
            return dateFormat.parse(folderName);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void deleteFolder(File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    deleteFolder(file);
                }
            }
        }

        // 删除文件夹
        if (!folder.delete()) {
            log.error("文件夹删除失败: {}", folder.getAbsolutePath());
        }else {
            log.info("文件夹删除成功: {}", folder.getAbsolutePath());
        }
    }

    public static void main(String[] args) {
        String rootFolderPath = "D:\\tmp\\test2";  // 将路径替换为你的文件夹路径
        File earliestFolder = findEarliestFolder(rootFolderPath);
        if (earliestFolder != null) {
            System.out.println("最早的文件夹: " + earliestFolder.getAbsolutePath());
        } else {
            System.out.println("未找到文件夹");
        }
        deleteFolder(earliestFolder);
    }

}
